<?php
namespace app\common\libs;

class WeixinTpl
{
	/*
	 * 获取微信token
	 */
	static function text($from, $to, $message) {
	    $xml = '<xml>
		<ToUserName><![CDATA[%s]]></ToUserName>
		<FromUserName><![CDATA[%s]]></FromUserName>
		<CreateTime>%d</CreateTime>
		<MsgType><![CDATA[text]]></MsgType>
		<Content><![CDATA[%s]]></Content>
		</xml>';

		return sprintf($xml, $to, $from, time(), $message);
	}

	static function img($from, $to, $message) {
		$xml = '<xml>
		<ToUserName><![CDATA[%s]]></ToUserName>
		<FromUserName><![CDATA[%s]]></FromUserName>
		<CreateTime>%d</CreateTime>
		<MsgType><![CDATA[image]]></MsgType>
		<Image>
		<MediaId><![CDATA[%s]]></MediaId>
		</Image>
		</xml>';
		return sprintf($xml, $to, $from, time(), $message);
	}

	static function textImg() {
		$sql = '<xml>
		<ToUserName><![CDATA[toUser]]></ToUserName>
		<FromUserName><![CDATA[fromUser]]></FromUserName>
		<CreateTime>12345678</CreateTime>
		<MsgType><![CDATA[news]]></MsgType>
		<ArticleCount>2</ArticleCount>
		<Articles>
		<item>
		<Title><![CDATA[title1]]></Title> 
		<Description><![CDATA[description1]]></Description>
		<PicUrl><![CDATA[picurl]]></PicUrl>
		<Url><![CDATA[url]]></Url>
		</item>
		<item>
		<Title><![CDATA[title]]></Title>
		<Description><![CDATA[description]]></Description>
		<PicUrl><![CDATA[picurl]]></PicUrl>
		<Url><![CDATA[url]]></Url>
		</item>
		</Articles>
		</xml>';
	}

	/*
	 * 客服图文介绍模板
	 */
	static function kefuNewsTpl($openid, $media_id) {
		return [
		    "touser" => $openid,
		    "msgtype"=>"mpnews",
		    "mpnews" => [
		         "media_id" => $media_id
		    ]
		];
	}

	static function kefuTextTpl($toUserid, $text) {
		return [
			    "touser"=>$toUserid,
			    "msgtype"=>"text",
			    "text"=>
			    [
			         "content"=> $text
			    ]
		];
	}

	/*
	 * 微信会员开通，模板提示消息
	 * data = [
		'openid'=>'','nickname'=>'','mobile'=>'','money'=>'',
	 ]
	 */
	static function tplRegisterNotice($data, $url='') {
		$url = empty($url) ? config('weixin.member_url') : $url;
		return json_encode([
		'touser' => $data['openid'],
		'template_id' => config('weixin.tpl_notice')['register'],
		'url' => $url,
		'data' => [
			'first' 	=> ['value'	=>"[咿咿呀呀]因您而精彩\r--------------------------------"],
			'keyword1' 	=> ['value'	=>$data['nickname']],
			'keyword2' 	=> ['value'	=>substr_replace($data['mobile'],'****', 3, 4)],
			'keyword3' 	=> ['value'	=>'会员'],
			'keyword4' 	=> ['value'	=>$data['money'].'元'],
			'remark' 	=> ['value'	=>"--------------------------------\r地址：".config('websys.shop')['address']],
		],
		], JSON_UNESCAPED_UNICODE);
	}

	/*
	 * 微信会员消费，模板提示消息
	 * data = [
		'openid'=>'','nickname'=>'','mobile'=>'','money'=>'','cost'=>''
	 ]
	 */
	static function tplCostNotice($data, $url='') {
		$url = empty($url) ? config('weixin.member_url') : $url;
		return json_encode([
		'touser' => $data['openid'],
		'template_id' => config('weixin.tpl_notice')['consumption'],
		'url' => $url,
		'data' => [
			'first' 	=> ['value'	=>"尊敬的{$data['nickname']}，消费详情如下\r--------------------------------"],
			'keyword1' 	=> ['value'	=>substr_replace($data['mobile'],'****', 3, 4)],
			'keyword2' 	=> ['value'	=>$data['cost']],
			'keyword3' 	=> ['value'	=>$data['money']],
			'keyword4' 	=> ['value'	=>'咿咿呀呀太阳城店'],
			'keyword5' 	=> ['value'	=>date('Y-m-d H:i:s')],
			'remark' 	=> ['value'	=>"--------------------------------\r咿咿呀呀祝您生活愉快"],
		],
		], JSON_UNESCAPED_UNICODE);
	}

	/*
	 * 微信会员充值，模板提示消息
	 * data = [
		'openid'=>'','nickname'=>'','mobile'=>'','money'=>'','cost'=>''
	 ]
	 */
	static function tplRechargeNotice($data, $url='') {
		$url = empty($url) ? config('weixin.member_url') : $url;
		return json_encode([
		'touser' => $data['openid'],
		'template_id' => config('weixin.tpl_notice')['recharge'],
		'url' => $url,
		'data' => [
			'first' 	=> ['value'	=>"尊敬的{$data['nickname']}，充值详情如下\r--------------------------------"],
			'keyword1' 	=> ['value'	=>$data['recharge_total'].'元'],
			'keyword2' 	=> ['value'	=>$data['recharge'].'元'],
			'keyword3' 	=> ['value'	=>$data['give'].'元'],
			'keyword4' 	=> ['value'	=>$data['total'].'元'],
			'remark' 	=> ['value'	=>"--------------------------------\r咿咿呀呀祝您生活愉快"],
		],
		], JSON_UNESCAPED_UNICODE);
	}

	/*
	 * 微信会员充值，模板提示消息
	 * data = [
		'openid'=>'','nickname'=>'','mobile'=>'','money'=>'','cost'=>''
	 ]
	 */
	static function tplTimesRechargeNotice($data, $url='') {
		$url = empty($url) ? config('weixin.member_url') : $url;
		return json_encode([
		'touser' => $data['openid'],
		'template_id' => config('weixin.tpl_notice')['times_recharge'],
		'url' => $url,
		'data' => [
			'first' 	=> ['value'	=>"尊敬的{$data['nickname']}，充值详情如下\r--------------------------------"],
			'keyword1' 	=> ['value'	=>substr_replace($data['mobile'],'****', 3, 4)],
			'keyword2' 	=> ['value'	=>$data['recharge_times'].'次'],
			'keyword3' 	=> ['value'	=>$data['times'].'次'],
			'remark' 	=> ['value'	=>"--------------------------------\r咿咿呀呀祝您生活愉快"],
		],
		], JSON_UNESCAPED_UNICODE);
	}
}